package com.heima.wemedia.listener;

import com.heima.common.constant.DelayConstants;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @Auther mr.IIE
 * @Date 2025/6/29 19:35
 */
@Component
@Slf4j
public class DelayListener {

    @Autowired
    private WmNewsService wmNewsService;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = DelayConstants.WM_NEWS_DELAY_QUEUE, durable = "true"),
                    exchange = @Exchange(name = DelayConstants.WM_NEWS_DELAY_DIRECT,delayed = "true", type = ExchangeTypes.DIRECT, durable = "true"),
                    key = DelayConstants.WM_NEWS_DELAY_KEY
            ))
    public void delayPush(String  message){
        // 根据消息传递的id查询对应的新闻
        WmNews wmNews = wmNewsService.lambdaQuery().eq(WmNews::getId, message).one();
        // 将新闻状态改为已发布
        wmNews.setStatus((short) 9);
        wmNews.setReason("发布成功");
        // 更新数据库中的信息
        boolean res = wmNewsService.updateById(wmNews);
        if (!res){
            throw new RuntimeException("修改文章状态失败");
        }
    }
}
